package com.example.dao.impl;

import com.example.dao.FollowDao;
import com.example.entity.customerAdmin.Follow;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FollowDaoImpl implements FollowDao {
    @Override
    public int add (Follow follow) {
        int i=-1;
        try{
        Object[] objects=new Object[9];
        objects[0]=follow.getType ();
        objects[1]=follow.getMode ();
        objects[2]=follow.getTime ();
        objects[3]=follow.getRemarks ();
        objects[4]=follow.getCusName ();
        objects[5]=follow.getCusType ();
        objects[6]=follow.getContactUser ();
        objects[7]=follow.getEnclosure ();
        objects[8]=follow.getRemindWho ();
        String sql="insert into follow(type,mode,time,remarks,cusName,cusType,contactUser," +
                "enclosure,remindWho) value(?,?,?,?,?,?,?,?,?)";
        i= BaseDao.modify (sql,objects);
    }catch (Exception e){
        e.printStackTrace ();
    }
        return i;

    }

    @Override
    public int del (int id) {
        int i=-1;
        Object[] objects=new Object[1];
        objects[0]=id;
        try {
           i= BaseDao.modify ("delete from follow where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int update (Follow follow) {
        int i=-1;
        try {
            Object[] objects=new Object[11];
            objects[0]=follow.getType ();
            objects[1]=follow.getMode ();
            objects[2]=follow.getTime ();
            objects[3]=follow.getRemarks ();
            objects[4]=follow.getCusName ();
            objects[5]=follow.getCusType ();
            objects[6]=follow.getContactUser ();
            objects[7]=follow.getEnclosure ();
            objects[9]=follow.getRemindWho ();
            objects[10]=follow.getId ();
            String sql="update contact_user set type=?,mode=?,time=?,remarks=?,cusName=?,cusType=?,contactUser=?,enclosure=?,remindWho=? where id=?";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public List<Follow> selectAll () {
        List<Follow> list=new ArrayList<> ();
        ResultSet select = BaseDao.select ("select * from follow ", null);
        try {
            while (select.next ()){
                Follow follow=new Follow ();
                follow.setId (select.getInt ("id"));
                follow.setType (select.getString ("type"));
                follow.setMode (select.getString ("mode"));
                follow.setTime (select.getString ("time"));
                follow.setRemarks (select.getString ("remarks"));
                follow.setCusName (select.getString ("cusName"));
                follow.setCusType (select.getString ("cusType"));
                follow.setContactUser (select.getString ("contactUser"));
                follow.setEnclosure (select.getString ("enclosure"));
                follow.setRemindWho (select.getString ("remindWho"));
                list.add (follow);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return list;
    }

    @Override
    public Follow selectById (int id)  {
        Follow follow=new Follow ();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from follow where id=?", objects);
        try {
            while (select.next ()){
                follow.setId (select.getInt ("id"));
                follow.setType (select.getString ("type"));
                follow.setMode (select.getString ("mode"));
                follow.setTime (select.getString ("time"));
                follow.setRemarks (select.getString ("remarks"));
                follow.setCusName (select.getString ("cusName"));
                follow.setCusType (select.getString ("cusType"));
                follow.setContactUser (select.getString ("contactUser"));
                follow.setEnclosure (select.getString ("enclosure"));
                follow.setRemindWho (select.getString ("remindWho"));
                return follow;
            }

        }catch (Exception e){
            e.printStackTrace ();
        }
        return null;
    }

    @Override
    public String selectUpdateTime(String name) {
        String time=null;
        Object[]objects=new Object[1];
        objects[0]=name;
        ResultSet resultSet = BaseDao.select("select time from follow where cusName=? order by time desc limit 1", objects);
        try {
            while (resultSet.next()){
                time=resultSet.getString("time");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return time;
    }

    @Override
    public String selectCreateTime(String name) {
        String time=null;
        Object[]objects=new Object[1];
        objects[0]=name;
        ResultSet resultSet = BaseDao.select("select time from follow where cusName=? order by time limit 1", objects);
        try {
            while (resultSet.next()){
                time=resultSet.getString("time");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return time;
    }

    @Override
    public String selectFolConInfo(String name) {
        String newContact=null;
        Object[]objects=new Object[1];
        objects[0]=name;
        ResultSet resultSet = BaseDao.select("select contactUser from follow where cusName=? order by time desc limit 1", objects);
        try {
            while (resultSet.next()){
              newContact=resultSet.getString("contactUser");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return newContact;
    }

    @Override
    public String selectprevConInfo(String name) {
        String prevContact=null;
        Object[]objects=new Object[1];
        objects[0]=name;
        ResultSet resultSet = BaseDao.select("select contactUser from follow where cusName=? group by contactUser limit 1,1", objects);
        try {
            while (resultSet.next()){
                prevContact=resultSet.getString("contactUser");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return prevContact;
    }
}
